// --------------------------------
// <copyright file="TableCollection.cs" company="TRONSoft">
//     Copyright (c) TRONSoft.
// </copyright>
// <license>
//   This source code is subject to terms and conditions of the Microsoft
//   Public License. A copy of the license can be found in the License.html
//   file at the root of this distribution. If you cannot locate the
//   Microsoft Public License, please send an email to dlr@microsoft.com.
//   By using this source code in any fashion, you are agreeing to be bound 
//   by the terms of the Microsoft Public License. You must not remove this
//   notice, or any other, from this software.
// </license>
// <author>Tron</author>
// ---------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Xml;

namespace Tronsoft.Actucal.Tables
{
    public class TableCollection : InternalDataCollectionBase
    {
        private readonly DataSet dsTables = new DataSet();
        private readonly ArrayList tables = new ArrayList();
        private readonly Dictionary<string, TableDefinition> tablesFromName = new Dictionary<string, TableDefinition>();

        internal TableCollection()
        {
        }

        public TableDefinition this[string name]
        {
            get
            {
                if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name");

                foreach (KeyValuePair<string, TableDefinition> pair in tablesFromName)
                {
                    if (pair.Key.ToLowerInvariant() == name.ToLowerInvariant())
                    {
                        return pair.Value;
                    }
                }

                return null;
            }
        }

        public TableDefinition this[int index]
        {
            get { return tables[index] as TableDefinition; }
        }

        protected override ArrayList List
        {
            get { return tables; }
        }

        public TableDefinition Add(string tableName)
        {
            if (string.IsNullOrEmpty(tableName))
                throw new ArgumentNullException("tableName");

            TableDefinition td;
            if (!Contains(tableName))
            {
                td = new TableDefinition(dsTables.Tables.Add(tableName));
                tables.Add(td);
                tablesFromName.Add(td.TableName, td);
            }
            else
            {
                td = this[tableName];
            }
            
            return td;
        }

        public bool Contains(string name)
        {
            if (string.IsNullOrEmpty(name))
                return false;

            foreach (DataTable dt in dsTables.Tables)
            {
                if (dt.TableName.ToLowerInvariant() == name.ToLowerInvariant())
                    return true;
            }

            return false;
        }

        public void Clear()
        {
            dsTables.Clear();
            dsTables.Tables.Clear();
            tables.Clear();
            tablesFromName.Clear();
        }

        #region WriteXml
        public void WriteXml(Stream stream)
        {
            dsTables.WriteXml(stream);
        }

        public void WriteXml(TextWriter writer)
        {
            dsTables.WriteXml(writer);
        }

        public void WriteXml(XmlWriter writer)
        {
            dsTables.WriteXml(writer);
        }

        public void WriteXml(string fileName)
        {
            dsTables.WriteXml(fileName);
        }

        public void WriteXml(Stream stream, XmlWriteMode mode)
        {
            dsTables.WriteXml(stream, mode);
        }

        public void WriteXml(TextWriter writer, XmlWriteMode mode)
        {
            dsTables.WriteXml(writer, mode);
        }

        public void WriteXml(XmlWriter writer, XmlWriteMode mode)
        {
            dsTables.WriteXml(writer, mode);
        }

        public void WriteXml(string fileName, XmlWriteMode mode)
        {
            dsTables.WriteXml(fileName, mode);
        } 
        #endregion

        #region ReadXml
        public XmlReadMode ReadXml(Stream stream)
        {
            return dsTables.ReadXml(stream);
        }

        public XmlReadMode ReadXml(XmlReader reader)
        {
            return dsTables.ReadXml(reader);
        }

        public XmlReadMode ReadXml(TextReader reader)
        {
            return dsTables.ReadXml(reader);
        }

        public XmlReadMode ReadXml(string fileName)
        {
            return dsTables.ReadXml(fileName);
        }

        public XmlReadMode ReadXml(Stream stream, XmlReadMode mode)
        {
            return dsTables.ReadXml(stream, mode);
        }

        public XmlReadMode ReadXml(XmlReader reader, XmlReadMode mode)
        {
            return dsTables.ReadXml(reader, mode);
        }

        public XmlReadMode ReadXml(TextReader reader, XmlReadMode mode)
        {
            return dsTables.ReadXml(reader, mode);
        }

        public XmlReadMode ReadXml(string fileName, XmlReadMode mode)
        {
            return dsTables.ReadXml(fileName, mode);
        } 
        #endregion
    }
}